package com.example.mavendemo.controller;

import jakarta.servlet.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/a")
@RestController
public class HelloController {
    @RequestMapping (value = "/satHi",method = RequestMethod.GET)
    public String satHi() {
        return "hello,java";
    }

    @RequestMapping("/m1")
    public String m1(String name) {
        return "接收到的name：" + name;
    }

    @RequestMapping("/m2")
    public String m2(String name, Integer age) {
        return "接收到的name：" + name + ",age：" + age;
    }

    @RequestMapping("/m3")
    public String m3(String name, int age) {
        return "接收到的name：" + name + ",age：" + age;
    }

    @RequestMapping("/m4")
    public String m4(Person person) {
        return "接收到的name：" + person.getName() + ",age：" + person.getAge();
    }

    @RequestMapping("/m5")
    public String m5(@RequestParam("name") String username) {
        return "接收到的name：" + username;
    }

    @RequestMapping("/m6")
    public String m6(@RequestParam(value = "name",required = false) String username) {
        return "接收到的name：" + username;
    }

    @RequestMapping("/m7")
    public String m7(String[] usernames) {
        return "接收到的username：" + Arrays.toString(usernames) + ",长度为：" + usernames.length ;
    }

    @RequestMapping("/m8")
    public String m8(List<String> lists) {
        return "接收到的username：" + lists + ",长度为：" + lists.size() ;
    }

    @RequestMapping("/m9")
    public String m9(@RequestBody Person person) {
        return "接收到的person：" + person.toString();
    }

    @RequestMapping("/m10/{userId}")
    public String m10(@PathVariable("userId") String id) {
        return "接收到的id：" + id;
    }

    @RequestMapping("/m11/{userId}/{username}")
    public String m11(@PathVariable("userId") String id,@PathVariable("username") String name) {
        return "接收到的id：" + id + ",name：" + name;
    }

    @RequestMapping("/m12")
    public String m12(@RequestPart MultipartFile file) throws IOException {
        file.transferTo(new File("D:/tmp/" + file.getOriginalFilename()) );
        System.out.println(file.getOriginalFilename());
        return "success";
    }

    @RequestMapping("/getCookie1")
    public String getCookie1(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "success";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String bite) {
        System.out.println("bite:" + bite);
        return "success";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();//默认为ture
        session.setAttribute("username","zhangsan");
        return "success";
    }

    @RequestMapping("/getSession1")
    public String getSession1(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session != null) {
            String name = "登录名称为：" + session.getAttribute("username");
            return name;
        }
        return "session为空";
    }

    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute String username) {
        return "username:" + username;
    }

    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session) {
        String name = (String)session.getAttribute("username");
        return "username:" + name;
    }

    @RequestMapping("/getHeader1")
    public String getHeader1(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:" + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent:" + userAgent;
    }


}


